package com.shuoyue.ycgk;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.ahammertest.ycgk.jpush.JPushHelper;
import com.dueeeke.videoplayer.BuildConfig;
import com.dueeeke.videoplayer.ijk.IjkPlayerFactory;
import com.dueeeke.videoplayer.player.VideoView;
import com.dueeeke.videoplayer.player.VideoViewConfig;
import com.dueeeke.videoplayer.player.VideoViewManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.shuoyue.ycgk.dkutils.ProgressManagerImpl;
import com.shuoyue.ycgk.umeng.UmUtils;
import com.shuoyue.ycgk.utils.GlideAlbumLoader;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.socialize.PlatformConfig;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumConfig;

import java.util.Locale;

import cn.jpush.android.api.JPushInterface;

/**
 * app
 * Created by dueeeke on 2017/4/22.
 */

public class MyApplication extends Application {

    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initDkPlayer();
        UmUtils.preInit();
        initAlubm();
        regToWx();
        initJpush();
        initBuggly();
    }


    void initBuggly() {
        Context context = getApplicationContext();
// 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
// 初始化Bugly
        CrashReport.initCrashReport(context, "85ac4b2ab3", true, strategy);
// 如果通过“AndroidManifest.xml”来配置APP信息，初始化方法如下
// CrashReport.initCrashReport(context, strategy);
    }

    public IWXAPI api;

    private void regToWx() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID, true);

        // 将应用的appId注册到微信
        api.registerApp(Constant.APP_ID);

        //建议动态监听微信启动广播进行注册到微信
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // 将该app注册到微信
                api.registerApp(Constant.APP_ID);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));

    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.alph0,
                    R.color.themeColor);//全局设置主题颜色
//            layout.setEnableHeaderTranslationContent(false);
//            RefreshHeader header = new TaurusHeader(context);
//            return header;//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            return new ClassicsHeader(context);
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            layout.setEnableFooterTranslationContent(false);
            layout.setEnableLoadMore(false);
            return new ClassicsFooter(context).setDrawableSize(20);
//            return new FalsifyFooter(context);

        });
    }

    public void initDkPlayer() {
        //播放器配置，注意：此为全局配置，按需开启
        VideoViewManager.setConfig(VideoViewConfig.newBuilder()
                .setLogEnabled(BuildConfig.DEBUG)//调试的时候请打开日志，方便排错
                .setPlayerFactory(IjkPlayerFactory.create())
//                .setPlayerFactory(ExoMediaPlayerFactory.create())
//                .setRenderViewFactory(SurfaceRenderViewFactory.create())
                .setEnableOrientation(true)
                .setEnableAudioFocus(false)
                .setScreenScaleType(VideoView.SCREEN_SCALE_DEFAULT)
                .setAdaptCutout(false)
                .setPlayOnMobileNetwork(true)
                .setProgressManager(new ProgressManagerImpl())
                .build());
    }

    private void initAlubm() {
        Album.initialize(AlbumConfig.newBuilder(this)
                .setAlbumLoader(new GlideAlbumLoader())
                .setLocale(Locale.getDefault())
                .build());
    }

    {
//WEIXIN_CIRCLE ,WEIXIN,QQ,QZONE
        String FileProvider = "com.ahammertest.ycgk.FileProvider";
        PlatformConfig.setWeixin("wxc0a19d8842a79634", "c4a4d4144f083fbdace9d254513d0a23");//"c4a4d4144f083fbdace9d254513d0a23"
        PlatformConfig.setWXFileProvider(FileProvider);
//        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad", "http://sns.whalecloud.com");
//        PlatformConfig.setSinaFileProvider(FileProvider);
        PlatformConfig.setQQZone("1106825685", "UW673BwDs8ANIaYu");
        PlatformConfig.setQQFileProvider("com.ahammertest.ycgkprovider.ImageFileProvider");
        PlatformConfig.setQQFileProvider(FileProvider);
    }

    void initJpush() {
        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
//        JPushInterface.init(this);
        JPushHelper.getInstance().init(this);
    }

    public static MyApplication getInstance() {
        return instance;
    }
}
